package com.ruoyi.project.purchase.service.impl;

import java.util.List;

import com.ruoyi.project.information.domain.TbCustomers;
import com.ruoyi.project.information.mapper.TbCustomersMapper;
import com.ruoyi.project.purchase.domain.pojo.ParamBean;
import com.ruoyi.project.warehouse.domain.RyDepotItem;
import com.ruoyi.project.warehouse.domain.RyInventoryCurrentStock;
import com.ruoyi.project.warehouse.domain.WareCurrentStock;
import com.ruoyi.project.warehouse.mapper.OutRyDepotItemMapper;
import com.ruoyi.project.warehouse.service.IWareCurrentStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.purchase.mapper.RyDepotDetailMapper;
import com.ruoyi.project.purchase.domain.RyDepotDetail;
import com.ruoyi.project.purchase.service.IRyDepotDetailService;

/**
 * 采供总清单明细Service业务层处理
 *
 * @author you
 * @date 2022-09-23
 */
@Service
public class RyDepotDetailServiceImpl implements IRyDepotDetailService
{
    @Autowired
    private RyDepotDetailMapper ryDepotDetailMapper;

    @Autowired
    private TbCustomersMapper tbCustomersMapper;

    @Autowired
    private IWareCurrentStockService wareCurrentStockService;

    @Autowired
    private OutRyDepotItemMapper outRyDepotItemMapper;

    /**
     * 查询采供总清单明细
     *
     * @param id 采供总清单明细主键
     * @return 采供总清单明细
     */
    @Override
    public RyDepotDetail selectRyDepotDetailById(Long id)
    {
        return ryDepotDetailMapper.selectRyDepotDetailById(id);
    }

    /**
     * 根据采购订单id查询子表信息
     */
    public List<RyDepotDetail>  selectBypurchaseOrderId(Long id){
        return ryDepotDetailMapper.selectBypurchaseOrderId(id);
    }


    /**
     * 查询可以生成订单的项目明细数据：就是purchase_order_id为空的
     * @param paramBean
     * @return
     */
    public List<RyDepotDetail> selectDetailForOrder(ParamBean paramBean){
        Long headerId = null;
        String supply = "";
        if(paramBean.getLinkHeadId() != null){//项目总清单id
            headerId = paramBean.getLinkHeadId();
        }
        if(paramBean.getOrganId() != null){//供应商
            //查询供应商信息
            TbCustomers tbCustomers = tbCustomersMapper.selectTbCustomersById(paramBean.getOrganId());
            supply = tbCustomers.getCompanyName();
        }
        List<RyDepotDetail> resList = ryDepotDetailMapper.selectDetailForOrder(headerId,supply);
        for(RyDepotDetail ryDepotDetail : resList){
            //根据物品id和总仓库id查询总库库物品当前库存量以及相关信息(总库id为1)
            RyInventoryCurrentStock ryInventoryCurrentStock = new RyInventoryCurrentStock();
            ryInventoryCurrentStock.setInventoryId(ryDepotDetail.getInventoryId());//物品id
            ryInventoryCurrentStock.setDepotId(1L);//仓库id
            WareCurrentStock wareCurrentStock = wareCurrentStockService.getInfoByDepotInventoryId(ryInventoryCurrentStock);
            if(wareCurrentStock == null){
                ryDepotDetail.setUsableNum(Double.valueOf(0));//总库可用数量
            } else {
                ryDepotDetail.setUsableNum(wareCurrentStock.getCurrentNumber());//总库可用数量
            }
        }

        return resList;
    }


    /**
     * 根据条件查询项目明细数据
     * @param paramBean
     * @return
     */
    public List<RyDepotDetail> selectDetailByParam(ParamBean paramBean){
        RyDepotDetail param = new RyDepotDetail();
        if(paramBean.getLinkHeadId() != null){//项目总清单id
            param.setHeaderId(paramBean.getLinkHeadId());
        }
        if(paramBean.getOrganId() != null){//供应商
            //查询供应商信息
            TbCustomers tbCustomers = tbCustomersMapper.selectTbCustomersById(paramBean.getOrganId());
            param.setSupply(tbCustomers.getCompanyName());
        }
        if(paramBean.getInventoryName() != null) {//物品名
            param.setInventoryName(paramBean.getInventoryName());
        }
        List<RyDepotDetail> resList = ryDepotDetailMapper.selectRyDepotDetailList(param);
        for(RyDepotDetail ryDepotDetail : resList){
            //设置查询参数为仓库id和物品id
            RyDepotItem ryDepotItem = new RyDepotItem();
            ryDepotItem.setDepotId(ryDepotDetail.getOutDepotId());//仓库id
            ryDepotItem.setInventoryId(ryDepotDetail.getInventoryId());//物品id
            //查询已出库数量，在查询过程中已做了处理：如果没有出库记录则查询结果为null，自动将null转为0
            Double haveOutNum = outRyDepotItemMapper.getNumByDepotInventoryId(ryDepotItem);
            ryDepotDetail.setHavaOutNum(haveOutNum);

            //展示出库数据时需要展示总入库数量，以便为后续出库操作作为参考
            //为防止未入库先出库的操作，在这里做一次判断，如果总入库数不存在，则设置为0
            if(ryDepotDetail.getInNum() == null){
                ryDepotDetail.setInNum(Double.valueOf(0));
            }
        }

        return resList;
    }


    /**
     * 查询采供总清单明细列表
     *
     * @param ryDepotDetail 采供总清单明细
     * @return 采供总清单明细
     */
    @Override
    public List<RyDepotDetail> selectRyDepotDetailList(RyDepotDetail ryDepotDetail)
    {
        return ryDepotDetailMapper.selectRyDepotDetailList(ryDepotDetail);
    }

    /**
     * 新增采供总清单明细
     *
     * @param ryDepotDetail 采供总清单明细
     * @return 结果
     */
    @Override
    public int insertRyDepotDetail(RyDepotDetail ryDepotDetail)
    {
        return ryDepotDetailMapper.insertRyDepotDetail(ryDepotDetail);
    }

    /**
     * 修改采供总清单明细
     *
     * @param ryDepotDetail 采供总清单明细
     * @return 结果
     */
    @Override
    public int updateRyDepotDetail(RyDepotDetail ryDepotDetail)
    {
        return ryDepotDetailMapper.updateRyDepotDetail(ryDepotDetail);
    }

    /**
     * 批量删除采供总清单明细
     *
     * @param ids 需要删除的采供总清单明细主键
     * @return 结果
     */
    @Override
    public int deleteRyDepotDetailByIds(Long[] ids)
    {
        return ryDepotDetailMapper.deleteRyDepotDetailByIds(ids);
    }

    /**
     * 删除采供总清单明细信息
     *
     * @param id 采供总清单明细主键
     * @return 结果
     */
    @Override
    public int deleteRyDepotDetailById(Long id)
    {
        return ryDepotDetailMapper.deleteRyDepotDetailById(id);
    }
}
